package cn.aukw.todolist;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import java.util.List;
import java.util.stream.Collectors;

import cn.aukw.todolist.bean.ListBean;
import cn.aukw.todolist.utils.DBUtils;

public class TodoService extends Service {
    private DBUtils dbUtils;
    private static final int NOTIFICATION_ID = 1;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        dbUtils = new DBUtils(this);
        startForeground(NOTIFICATION_ID, buildNotification());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        updateNotification();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
    }

    private PendingIntent createPendingIntent() {
        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        int flags = PendingIntent.FLAG_UPDATE_CURRENT;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            flags |= PendingIntent.FLAG_IMMUTABLE;
        }

        return PendingIntent.getActivity(this, 0, notificationIntent, flags);
    }

    private Notification buildNotification() {
        String title = "待办事项提醒";
        String content = getTodoContent();

        return new NotificationCompat.Builder(this, "todo_channel_id")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(content)
                .setContentIntent(createPendingIntent())
                .setPriority(NotificationCompat.PRIORITY_LOW)
                .setOngoing(true)
                .setAutoCancel(false)
                .build();
    }

    private void updateNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, buildNotification());
    }

    private String getTodoContent() {
        List<ListBean> todos = dbUtils.queryList();
        List<ListBean> unfinishedTodos = todos.stream()
                .filter(todo -> !todo.isCompleted()) // 过滤出未完成的待办事项
                .collect(Collectors.toList()); // 收集到列表中

        if (unfinishedTodos.isEmpty()) {
            return "暂无待办事项";
        }

        StringBuilder sb = new StringBuilder();
        int count = 1; // 用于编号待办事项
        for (ListBean todo : unfinishedTodos) {
            sb.append(count++).append(". ").append(todo.getTitle()).append("\n"); // 添加编号和待办事项的标题，并在每个待办事项后添加换行符
        }

        return sb.toString();
    }
}